<!doctype html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="-技术 -面向对象语言," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="踏入面向对象的世界最近在观看Zepto源码解析的视频时，老师又讲到了$对象与普通对象的区别，还记得《锋利的jquery》一书中说，$对象与普通对象的区别在于，$对象是一个数组结构的对象，但没有数组的push，shift方法。想必在控制台大家也注意这对”[]”。那么它内部是如何实现这个不是数组的对象呢？其实实现这个方法非常容易，也就是这个方法，体现了JS作为一门面向对象编程语言的继承功能——JS原型">
<meta name="keywords" content="-技术 -面向对象语言">
<meta property="og:type" content="article">
<meta property="og:title" content="探索JS原型链">
<meta property="og:url" content="https://github.com/remarkableRide/io/2017/05/10/探索JS原型链/index.html">
<meta property="og:site_name" content="RemarkableRide">
<meta property="og:description" content="踏入面向对象的世界最近在观看Zepto源码解析的视频时，老师又讲到了$对象与普通对象的区别，还记得《锋利的jquery》一书中说，$对象与普通对象的区别在于，$对象是一个数组结构的对象，但没有数组的push，shift方法。想必在控制台大家也注意这对”[]”。那么它内部是如何实现这个不是数组的对象呢？其实实现这个方法非常容易，也就是这个方法，体现了JS作为一门面向对象编程语言的继承功能——JS原型">
<meta property="og:updated_time" content="2017-05-18T12:09:32.940Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="探索JS原型链">
<meta name="twitter:description" content="踏入面向对象的世界最近在观看Zepto源码解析的视频时，老师又讲到了$对象与普通对象的区别，还记得《锋利的jquery》一书中说，$对象与普通对象的区别在于，$对象是一个数组结构的对象，但没有数组的push，shift方法。想必在控制台大家也注意这对”[]”。那么它内部是如何实现这个不是数组的对象呢？其实实现这个方法非常容易，也就是这个方法，体现了JS作为一门面向对象编程语言的继承功能——JS原型">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://github.com/remarkableRide/io/2017/05/10/探索JS原型链/"/>





  <title> 探索JS原型链 | RemarkableRide </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  














  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">RemarkableRide</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocapitalize="off" autocomplete="off" autocorrect="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://github.com/remarkableRide/io/2017/05/10/探索JS原型链/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="remarkableRide">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="RemarkableRide">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                探索JS原型链
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-05-10T22:26:22+08:00">
                2017-05-10
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计</span>
                
                <span title="字数统计">
                  1,479
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长</span>
                
                <span title="阅读时长">
                  6
                </span>
              
            </div>
          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="踏入面向对象的世界"><a href="#踏入面向对象的世界" class="headerlink" title="踏入面向对象的世界"></a>踏入面向对象的世界</h2><p>最近在观看Zepto源码解析的视频时，老师又讲到了$对象与普通对象的区别，还记得《锋利的jquery》一书中说，$对象与普通对象的区别在于，$对象是一个数组结构的对象，但没有数组的push，shift方法。想必在控制台大家也注意这对”[]”。那么它内部是如何实现这个不是数组的对象呢？其实实现这个方法非常容易，也就是这个方法，体现了JS作为一门面向对象编程语言的继承功能——JS原型链。</p>
<h3 id="JS原型英文解析"><a href="#JS原型英文解析" class="headerlink" title="JS原型英文解析"></a>JS原型英文解析</h3><p>Every JavaScript object has a prototype. The prototype is also an object.<br>All JavaScript objects inherit their properties and methods from their prototype.</p>
<p><strong>JavaScript Prototypes</strong><br>All JavaScript objects inherit the properties and methods from their prototype.<br>Objects created using an object literal, or with new Object(), inherit from a prototype called Object.prototype.<br>Objects created with new Date() inherit the Date.prototype.<br>The Object.prototype is on the top of the prototype chain.<br>All JavaScript objects (Date, Array, RegExp, Function, ….) inherit from the Object.prototype.<br>More info: <a href="http://w3schools.bootcss.com/js/js_object_prototypes.html" target="_blank" rel="external">w3schools</a><br>首先说下为什么要拉出英文的定义，我在写这篇文档时，查看了前3页的搜索“JS原型链”的文章，其中定义最清晰简洁的还要属w3school，一般来说访问这个网站需要翻墙，国内有一个汉化版，但是不全，比较新的内容没有搬过来，比如英文版的w3school有一个w3.css的小框架，用法跟bootstrap一样，都是往class标签里添加类，用起来比bootstrap更小巧。不过比较冷门。<br>引用的英文的意思是说，每个JS对象都有一个原型，原型也是对象，JS对象从原型那里继承了属性和方法。既然每个对象都有原型，那肯定有一个顶，也就是原型链最深处的原始原型，下文也说了，即<strong>Object.prototype</strong>，但，这里有一个常见的误区，就是把原型和对象的prototype属性划等号。在ECMAScript5的语法当中，prototype是函数特有的属性，实例对象的原型则是<code>__proto__</code>,<code>prototype</code>用来存放函数继承给下一代的东西，但是直接访问是访问不到的。例一：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> a = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;&#125;;</div><div class="line">a.prototype.b=<span class="number">3</span>;</div><div class="line">a.b;   <span class="comment">//  undefined</span></div><div class="line"><span class="keyword">var</span> b = <span class="keyword">new</span> a();</div><div class="line">b.b;   <span class="comment">//  3</span></div></pre></td></tr></table></figure>
<p>这里用new关键字构造了b对象，此时a也称为构造函数，构造函数很容易和<code>constructor</code>属性弄混，尤其是：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> b = <span class="keyword">new</span> a () ;</div><div class="line">b.constructor === a;   <span class="comment">//  true</span></div></pre></td></tr></table></figure></p>
<p>这里返回的true让人觉得这是b由a构造的证据，but，实际上不是，<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">b.hasOwnProperty(<span class="string">"constructor"</span>);   <span class="comment">//  false</span></div></pre></td></tr></table></figure></p>
<p>b中并没有<code>constructor</code>属性，那这个属性是哪里来的呢，根据原型继承，JS会沿着原型链向上查找，首先是<code>a.prototype</code>:<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">b.__proto__ === a.prototype;   <span class="comment">//  true</span></div><div class="line">a.prototype.hasOwnProperty(<span class="string">"constructor"</span>);   <span class="comment">//  true</span></div><div class="line">b.constructor === a.prototype.constructor;   <span class="comment">//  true</span></div></pre></td></tr></table></figure></p>
<p>实际上<code>b.constructor</code>是a原型链上的属性，而a的原型链之所以有这个属性，也正是指出了a的原型链是a对象在实例化出来的同时由a对象构造出来存放继承属性及方法的地方。不过由于<code>constructor</code>属性可以随意更改，所以单凭该属性来判定函数原型的构造很危险，也并不常用。a函数对象的实例化：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> a = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;&#125;;</div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">a</span>(<span class="params"></span>) </span>&#123;&#125;;</div><div class="line"><span class="keyword">var</span> a = <span class="keyword">new</span> <span class="built_in">Function</span> () ;</div></pre></td></tr></table></figure></p>
<p>以上3种方法是等价的，唯一不同的是<code>function a () {}</code>函数声明会前置，这里涉及到JS引擎解析代码的过程，略过不提。<br>第3种方法也揭示了创建一个函数也是一个实例化，常用的<code>Array，String，Boolean</code>。实际上也是<code>Function</code>函数的实例化对象。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">Array</span>.__proto__ === <span class="built_in">Function</span>.prototype;   <span class="comment">//true</span></div><div class="line"><span class="built_in">Boolean</span>.__proto__ === <span class="built_in">Function</span>.prototype;   <span class="comment">//true</span></div></pre></td></tr></table></figure></p>
<p>常用的数组方法，push，pop，shift，也是定义在Array的Prototype上：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">console</span>.log(<span class="built_in">Array</span>.prototype);</div></pre></td></tr></table></figure></p>
<p>回到刚开始的问题：$()对象是类数组的结构，却没有数组的方法，即$()实际的原型不是指向Array函数的prototype。例：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> a = <span class="keyword">new</span> <span class="built_in">Array</span>();</div><div class="line">a.push(<span class="number">1</span>);   <span class="comment">//  1</span></div><div class="line">a.__proto__ = <span class="keyword">new</span> <span class="built_in">Object</span>();</div><div class="line">a.push(<span class="number">2</span>);   <span class="comment">//  Uncaught TypeError</span></div></pre></td></tr></table></figure></p>
<p>更改了a数组的原型指向，便没有了数组上定义的方法。</p>
<h3 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h3><p>函数的<code>prototype</code>对象存放着该函数实例化对象继承的方法，<code>Array</code>等函数的定义更像是实现了一个方法集。一个面向对象编程语言当中类的概念。但并不完美，譬如函数本身无法访问到，所以便有了JS当中的特权方法，也就是另一个难点<code>this</code>的指向，下一章，我将会对JS的作用域进行分析，想要理解<code>this</code>的指向问题，闭包，实际上就是理解函数在执行时的作用域的变化。<br><code>prototype</code>对象中若是定义了一个引用类型的方法或者是属性，当其中的一个实例进行了修改操作，其原型链上的所有实例都会收到影响，所以，引用类型的方法或者属性应该避免定义在原型上，可以改用this：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> a = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</div><div class="line">	a.prototype.array=[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>];</div><div class="line">&#125;</div><div class="line"><span class="keyword">var</span> b = <span class="keyword">new</span> a();</div><div class="line"><span class="keyword">var</span> c = <span class="keyword">new</span> a();</div><div class="line">b.array.push(<span class="number">4</span>);   <span class="comment">//  [1,2,3,4]</span></div><div class="line"><span class="built_in">console</span>.log(c);   <span class="comment">//  [1,2,3,4]</span></div></pre></td></tr></table></figure></p>
<h3 id="题外话"><a href="#题外话" class="headerlink" title="题外话"></a>题外话</h3><p>这篇文章从10号晚上开始写，今天14号。比预计的进度慢，期间严重感冒，一度坐在电脑面前咳咳咳，除了上班剩下的时间都用来睡觉恢复了。今天感冒消散，赶紧把挖的洞补上。想说的是，写博客确实是一个人能力提升的好办法，从写的时候上网查资料，到转化为自己理解的语言，再进行代码测试，最后发布。这种用输出倒逼输入的过程比自己看书，看视频学习的效率要高上不少，难怪有老的程序员说，想要提高，就要自己写博客。也希望自己能够越写越顺畅~</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        
  <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
    <div>听说打赏的人会中500万!</div>
    <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
      <span>赏</span>
    </button>
    <div id="QR" style="display: none;">
      
        <div id="wechat" style="display: inline-block">
          <img id="wechat_qr" src="/images/money.png" alt="remarkableRide WeChat Pay"/>
          <p>微信打赏</p>
        </div>
      
      
    </div>
  </div>


      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/技术-面向对象语言/" rel="tag"># -技术 -面向对象语言</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/05/03/博客添加功能/" rel="next" title="博客添加功能">
                <i class="fa fa-chevron-left"></i> 博客添加功能
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/05/18/JS作用域/" rel="prev" title="JS作用域">
                JS作用域 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="lv-container" data-id="city" data-uid="MTAyMC8yODQ3MS81MDQy"></div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.gif"
               alt="remarkableRide" />
          <p class="site-author-name" itemprop="name">remarkableRide</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">6</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">5</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/remarkableRide" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        
          
             <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=35032655&auto=0&height=66"></iframe>
         
        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#踏入面向对象的世界"><span class="nav-number">1.</span> <span class="nav-text">踏入面向对象的世界</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#JS原型英文解析"><span class="nav-number">1.1.</span> <span class="nav-text">JS原型英文解析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#总结"><span class="nav-number">1.2.</span> <span class="nav-text">总结</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#题外话"><span class="nav-number">1.3.</span> <span class="nav-text">题外话</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">remarkableRide</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  






  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.0"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  


  




	





  





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  






  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length == 0) {
      search_path = "search.xml";
    }
    var path = "/" + search_path;
    // monitor main search box;

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.popup').toggle();
    }
    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';
      $.ajax({
        url: path,
        dataType: "xml",
        async: true,
        success: function( xmlResponse ) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = $( "entry", xmlResponse ).map(function() {
            return {
              title: $( "title", this ).text(),
              content: $("content",this).text(),
              url: $( "url" , this).text()
            };
          }).get();
          var $input = document.getElementById(search_id);
          var $resultContent = document.getElementById(content_id);
          $input.addEventListener('input', function(){
            var matchcounts = 0;
            var str='<ul class=\"search-result-list\">';
            var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
            $resultContent.innerHTML = "";
            if (this.value.trim().length > 1) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var content_index = [];
                var data_title = data.title.trim().toLowerCase();
                var data_content = data.content.trim().replace(/<[^>]+>/g,"").toLowerCase();
                var data_url = decodeURIComponent(data.url);
                var index_title = -1;
                var index_content = -1;
                var first_occur = -1;
                // only match artiles with not empty titles and contents
                if(data_title != '') {
                  keywords.forEach(function(keyword, i) {
                    index_title = data_title.indexOf(keyword);
                    index_content = data_content.indexOf(keyword);
                    if( index_title >= 0 || index_content >= 0 ){
                      isMatch = true;
                      if (i == 0) {
                        first_occur = index_content;
                      }
                    }

                  });
                }
                // show search results
                if (isMatch) {
                  matchcounts += 1;
                  str += "<li><a href='"+ data_url +"' class='search-result-title'>"+ data_title +"</a>";
                  var content = data.content.trim().replace(/<[^>]+>/g,"");
                  if (first_occur >= 0) {
                    // cut out 100 characters
                    var start = first_occur - 20;
                    var end = first_occur + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if(start == 0){
                      end = 50;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    var match_content = content.substring(start, end);
                    // highlight all keywords
                    keywords.forEach(function(keyword){
                      var regS = new RegExp(keyword, "gi");
                      match_content = match_content.replace(regS, "<b class=\"search-keyword\">"+keyword+"</b>");
                    });

                    str += "<p class=\"search-result\">" + match_content +"...</p>"
                  }
                  str += "</li>";
                }
              })};
            str += "</ul>";
            if (matchcounts == 0) { str = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>' }
            if (keywords == "") { str = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>' }
            $resultContent.innerHTML = str;
          });
          proceedsearch();
        }
      });}

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched == false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(function(e){
      $('.popup').hide();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    });
    $('.popup').click(function(e){
      e.stopPropagation();
    });
  </script>





  

  

  

  

</body>
</html>
